﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using IAM.Interface.Model;
using System.Xml;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 权限描述信息
    /// </summary>
    public class ACISession : AbstractConvertObject,IConvertXml
    {
        #region 构造函数
        public ACISession()
        {
            _acisession = new System.Collections.ArrayList();
        }
        #endregion
        #region 属性
        private System.Collections.ArrayList _acisession = null;
        /// <summary>
        /// 已选择的权限配置项集合
        /// 【CheckedItem集合】
        /// </summary>
        public System.Collections.ArrayList  Items
        {
            get
            {
                return _acisession;
            }
            set
            {
                _acisession = value;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append("<ACISession>");
                foreach (System.Object checkeditem in this.Items)
                {
                    _xml.Append(((CheckedItem)checkeditem).XML);
                }

                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</ACISession>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                CheckedItem checkeditem = new CheckedItem();
                checkeditem.CurrentXmlNode = xn;
                checkeditem = (CheckedItem)checkeditem.ConvertToObject();
                this.Items.Add(checkeditem);
            }
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 ACISession 对象是否等效于当前 ACISession
        /// 1.验证类型是否一致
        /// 2.验证集合数量是否一致
        /// 3.验证集合中对象是否一致【无顺比较】
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            //1.验证类型是否一致
            ACISession acisession = null;
            try
            {
                acisession = (ACISession)obj;
            }
            catch
            {
                return false;
            }
            //2.验证集合数量是否一致
            if (this.Items.Count != acisession.Items.Count)
            {
                return false;
            }

            bool boolforeach = true;
            //3.验证集合中对象是否一致【无顺比较】
            foreach (object rudeobj in this.Items)
            {
                boolforeach = false;
                foreach (object aimobj in acisession.Items)
                {
                    if (((CheckedItem)rudeobj).Equals(aimobj))
                    {
                        boolforeach = true;
                        break;
                    }
                }
                if (boolforeach == false)
                {
                    break;
                }
            }
            return boolforeach;

        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// 应为包含CheckedItem集合的顺序问题此函数不能作为对象的唯一标志
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
